﻿using WH.Model.Systems;
using WH.Common.Handler;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WH.Model.OrgContext
{
    public class OrganizationContext
    {
        //
        // 摘要:
        //     用户Id
        public string? UserId { get; }

        //
        // 摘要:
        //     角色Id
        public string? RoleId { get; }

        /// <summary>
        /// 是否是超管
        /// </summary>
        public bool IsAdmin { get; set; }

        //
        // 摘要:
        //     用户账号
        public string? UserAccount { get; }
        //
        // 摘要:
        //     用户名
        public string? UserName { get; }

        //
        // 摘要:
        //     机构Id
        public string? CurrentOrganizationId { get; }

        //
        // 摘要:
        //     机构Id集合
        public string[]? OrganizationIds { get; set; }


        private readonly ISqlSugarClient dbContext;

        public OrganizationContext(ISqlSugarClient _dbContext)
        {
            dbContext = _dbContext;

            if (!string.IsNullOrWhiteSpace(JwtTokenHandler.AccessToken))
            {
                var user = JwtTokenHandler.LoginModel;

                this.UserId = user?.UserId;
                this.RoleId = user?.RoleId;
                this.UserAccount = user?.UserAccount;
                this.UserName = user?.Name;
                this.IsAdmin = user?.IsAdmin ?? false;

                var organizationId = user?.OrganizationId;
                this.CurrentOrganizationId = organizationId;

                var list = dbContext.Queryable<OrganizationEntity>().Where(x => x.IsEnable).WithCache().ToList();

                if (user?.IsAdmin == true)
                {
                    var l = list.Select(x => x.Id).ToList();

                    l.Add("0");

                    this.OrganizationIds = l.ToArray();
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(organizationId))
                    {
                        var result = new List<OrganizationEntity>();

                        GetOrganizationIds(organizationId, list, result);

                        var organizeIds = new List<string> { organizationId };
                        organizeIds.AddRange(result.Select(o => o.Id));

                        this.OrganizationIds = organizeIds.ToArray();
                    }
                }
            }
        }


        private void GetOrganizationIds(string parentId, List<OrganizationEntity> list, List<OrganizationEntity> result)
        {
            if (list == null)
            {
                return;
            }

            var ss = list.Where(l => l.ParentId == parentId).ToList();
            if (ss.Any())
            {
                result.AddRange(ss);
                foreach (var s in ss)
                {
                    GetOrganizationIds(s.Id, list, result);
                }
            }
        }
    }
}
